package net.maku.project.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum;
import net.maku.project.convert.InfoConvert;
import net.maku.project.entity.InfoEntity;
import net.maku.project.service.InfoService;
import net.maku.project.query.InfoQuery;
import net.maku.project.vo.*;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 项目信息
*
* @author lvpb lv941226@gmail.com
* @since 1.0.0 2024-07-08
*/
@RestController
@RequestMapping("project/info")
@Tag(name="项目信息")
@AllArgsConstructor
public class InfoController {
    private final InfoService infoService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('project:info:page')")
    public Result<PageResult<InfoVO>> page(@ParameterObject @Valid InfoQuery query){
        PageResult<InfoVO> page = infoService.page(query);

        return Result.ok(page);
    }
    @PostMapping ("plz_page")
    @Operation(summary = "分页（小程序）")
//    @PreAuthorize("hasAuthority('project:info:plz_page')")
    public Result<PageResult<InfoWithStatesVO>> plzPage(@RequestBody SimpleSearchVO simpleSearchVO){
        PageResult<InfoWithStatesVO> page = infoService.plzPage(simpleSearchVO);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('project:info:info')")
    public Result<InfoVO> get(@PathVariable("id") Long id){
        InfoEntity entity = infoService.getById(id);
        infoService.calculateStatusDuration(entity);
        return Result.ok(InfoConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('project:info:save')")
    public Result<String> save(@RequestBody InfoVO vo){
        infoService.save(vo);
        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('project:info:update')")
    public Result<String> update(@RequestBody @Valid InfoVO vo){
        infoService.update(vo);
        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('project:info:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        infoService.delete(idList);
        return Result.ok();
    }

    @GetMapping("/code")
    public Result<InfoVO> getInfoByCode(@RequestParam(value = "code",required = false) String code){
        InfoVO infoVO = infoService.getInfoByCode(code);
        return Result.ok(infoVO);
    }

    @GetMapping("/id")
    public Result<InfoVO> getInfoById(@RequestParam(value = "id") Long id){
        InfoVO infoVO = infoService.getInfoById(id);
        return Result.ok(infoVO);
    }
    @GetMapping("/countInfo")
    public Result<ProjectCountVO> getInfoCount(){
        ProjectCountVO infoVO = infoService.getInfoCount();
        return Result.ok(infoVO);
    }
    @GetMapping("/timeout")
    public Result<List<TimeOutInfoVO>> getTimeOutInfo(){
        List<TimeOutInfoVO> infoVO = infoService.getTimeOutInfo();
        return Result.ok(infoVO);
    }

    @PostMapping("/list")
    public Result<PageResult<SimpleInfoVO>> getSimpleInfos(@RequestBody SimpleSearchVO simpleSearchVO){
        PageResult<SimpleInfoVO> result  = infoService.getSimpleInfos(simpleSearchVO);
        return Result.ok(result);
    }

    @GetMapping("export")
    @Operation(summary = "导出excel")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('project:info:export')")
    public void export() {
        infoService.export();
    }

}